/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.andremion.louvre.sample.slice;

import com.andremion.louvre.sample.ResourceTable;
import com.bumptech.glide.Glide;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.utils.net.Uri;

import java.util.List;

/**
 * ItemProvider Class
 */
public class ItemProvider extends BaseItemProvider {
    private static List<Uri> mUris;
    private List<String> mPaths;
    private Context mContext;

    public ItemProvider(Context context, List<Uri> mUris, List<String> mPaths) {
        mContext = context;
        this.mPaths = mPaths;
        this.mUris = mUris;
    }

    /**
     * setData
     *
     * @param uris  uris
     * @param paths paths
     */
    public void setData(List<Uri> uris, List<String> paths) {
        mUris = uris;
        mPaths = paths;
        notifyDataChanged();
    }

    /**
     * setData
     *
     * @param uris uris
     */
    public void setData(List<Uri> uris) {
        mUris.clear();
        mUris.addAll(uris);
        notifyDataChanged();
    }

    public List<Uri> getmUris() {
        return mUris;
    }

    @Override
    public int getCount() {
        return mUris == null ? 0 : mUris.size();
    }

    @Override
    public Object getItem(int item) {
        return mUris == null ? 0 : mUris.get(item);
    }

    @Override
    public long getItemId(int item) {
        return mUris == null ? 0 : mUris.get(item).hashCode();
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Component layout = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_uri_item,
                componentContainer, false);
        int screenWidth = AttrHelper.fp2px(componentContainer.getContext().getResourceManager()
                .getDeviceCapability().width, componentContainer.getContext());
        int size = screenWidth / 4;
        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(size, size);
        layout.setLayoutConfig(layoutConfig);
        Image mImageView = (Image) layout.findComponentById(ResourceTable.Id_image);
        mImageView.setLayoutConfig(layoutConfig);
        Glide.with(componentContainer.getContext())//配置上下文
                .load(Uri.parse(mUris.get(position).toString()))//设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                .into(mImageView);
        return layout;
    }
}
